package com.wx_shop.api.security;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.wx_shop.api.comm.ServiceCode;

/**
 * 未登录时返回json
 */
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {

	@Override
	public void commence(HttpServletRequest req, HttpServletResponse res, AuthenticationException ae) throws IOException,
			ServletException {
		
		JSONObject jsonObj = new JSONObject();
		try {
			jsonObj.put("status", ServiceCode.NO_LOGIN.status);
			jsonObj.put("message", ServiceCode.NO_LOGIN.message);
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		res.setStatus(HttpStatus.FORBIDDEN.value());
		res.setContentType("text/json");
		res.getWriter().println(jsonObj.toString());
		res.flushBuffer();
	}

}
